package com.project.mall.controller;

import com.project.mall.pojo.*;
import com.project.mall.service.ServiceH;
import com.project.mall.service.ServiceW;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@RequestMapping("before")
@Controller
public class Controllerw {
    @Resource
    private ServiceH serviceH;
    @Resource
    private ServiceW serviceW;
    //立即选购    //订单中心
    @RequestMapping("/ljxg/{gid}/{gtypeid}/{gcolorid}/{count}/{price}")
    public String ljxg(@PathVariable Integer gid,
                       @PathVariable Integer gtypeid,
                       @PathVariable Integer gcolorid,
                       @PathVariable Integer count,
                       @PathVariable Integer price, HttpServletRequest request,Model model){
//        System.out.println("进入ljxg");
        Integer userid = (Integer) request.getSession().getAttribute("userid");
        if(userid==null||userid==0){
           return "redirect:/login";
        }
        List<Address> addlist = serviceH.getAddress(userid);
        model.addAttribute("addlist",addlist);
        HashMap<Integer[],Boolean> map = new HashMap<>();
        Goodscolor color = serviceW.getcolorbygcolorid(gcolorid);
        Goodstype type = serviceW.gettypebygtypeid(gtypeid);
        Goods goods = serviceW.getGoods(gid);
        goods.setPrice(price);
        goods.setCount(count);
        /*随机数生成订单号*/
        int random=(int)(Math.random()*10000000+1);
//        System.out.println(random);
        goods.setUptime(new Date());
        model.addAttribute("color",color);
        model.addAttribute("type",type);
        model.addAttribute("goods",goods);
        model.addAttribute("random",random);

        return "before/ordercenter";
    }


    @RequestMapping("before/useraddress")
    public String useraddress(){
        return "before/user-address";
    }


    //查看评价
    @RequestMapping("showcomments/{gid}")
    public String comments(@PathVariable Integer gid, Model model){
        List<Comment> comment = serviceW.getComment(gid);
        Goods goods = serviceW.getGoods(gid);
        model.addAttribute("comments",comment);
        model.addAttribute("gooods",goods);
        return "before/show-comments";
    }




    //详情
    @RequestMapping("goodsinfo/{gid}")
    public String getgoodsinfo(@PathVariable Integer gid, Model model){
        List<Goods> goodsinfo = serviceW.getGoodsInfoByGid(gid);
        List<Showimages> showimages = serviceW.getPicList(gid);
        model.addAttribute("goodsinfo",goodsinfo);
        model.addAttribute("showimages",showimages);
        Goods goods = serviceW.getGoods(gid);
        model.addAttribute("goods",goods);
        return "before/goodsinfo";
    }
    //其他详情
/*    @RequestMapping("otherinfo/{gid}")
    public String getotherinfo(@PathVariable Integer gid, Model model){
        List<Goods> goodsinfo = serviceW.getGoodsInfoByGid(gid);
        model.addAttribute("goodsinfo",goodsinfo);
        Goods goods = serviceW.getGoods(gid);
        model.addAttribute("goods",goods);
        return "before/otherinfo";
    }*/

    //参数
    @RequestMapping("goodsparam/{gid}")
    public String getontherinfo(@PathVariable Integer gid, Model model){
        Applianceparam appliParm = serviceW.getAppliParm(gid);
        model.addAttribute("appliParms",appliParm);
        Goods goodsparam = serviceW.getGoodsParm(gid);
        model.addAttribute("goodsparam",goodsparam);;
        List<Goods> goodsinfo = serviceW.getGoodsInfoByGid(gid);
        model.addAttribute("goodsinfo",goodsinfo);
        return "before/goodsparam";
    }

/*   //家电参数
    @RequestMapping("appparams/{gid}")
    public String getparams(@PathVariable Integer gid,Model model){

        return "goodsparam";
    }*/

    //概述
    @RequestMapping("summarize/{gid}")
    public String getInfo(@PathVariable Integer gid,Model model){
        List<Goods> goods = serviceW.getGoodsInfoByGid(gid);
        List<Infoimages> infoimages = serviceW.getInfoList(gid);
        model.addAttribute("infoimages",infoimages);
        model.addAttribute("goods",goods);
        return "before/goodssummarize";
    }

}
